package com.qdlc.p2p.biz.tpp.model;

import java.io.IOException;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qdlc.p2p.common.constant.SinaConstant;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dto.TppSinaSplit;

/**
 * 新浪代付交易
 * @author zlhx
 * @version 2.0
 * @date 2015年8月12日 上午11:34:08
 */
public class SinaPayTrade extends SinaModel {

	public SinaPayTrade(){
		super();
	}
	/**
	 * 
	 * @param out_trade_code      交易码
	 * @param payee_identity_id   收款人标识
	 * @param amount			      金额
	 * @param summary             摘要
	 * @param goods_id			      商户标号		
	 * @param splitList           分账信息列表集合
	 * @param
	 */
	public SinaPayTrade(String payee_identity_id, String out_trade_code, String amount, 
			String summary, String goods_id, String out_trade_no, List<TppSinaSplit> splitList){
		this.out_trade_no = out_trade_no;
		this.out_trade_code = out_trade_code;
		this.payee_identity_id = payee_identity_id;
		this.payee_identity_type = SinaConstant.IDENTITY_TYPE_UID;
        // 商户 设置为基本型
        if(SystemConfigHelper.getValue(Nid.MEMBER_EMAIL).equals(payee_identity_id))
        {
            this.account_type = SinaConstant.ACCOUNT_TYPE_BASIC;
        } else {
            this.account_type = SinaConstant.ACCOUNT_TYPE_SAVING_POT;
        }
		this.amount = amount;
		this.split_list = TppSinaSplit.getSplitList(splitList, SystemConfigHelper.getValue(Nid.MEMBER_EMAIL));
		getParamNames(split_list);
		this.summary = summary;
		this.goods_id = goods_id;
		this.setService("create_single_hosting_pay_trade");
		this.setUrl_type(SinaConstant.URL_TYPE_CASH);
		this.setRequest_time(DateUtil.dateStr3(DateUtil.getNow()));
		this.setEncode(SinaConstant.ENCODE_TRUE);
		// 新浪回调地址
		String webUrl = SystemConfigHelper.getValue(Nid.WEB_URL);
		this.setReturn_url(webUrl+"/tpp/tppSinaAction/tppPayTradeReturn.html");
		this.setNotify_url(webUrl+"/tpp/tppSinaAction/tppPayTradeNotify.html");
	}
	
	/**
	 * 交易订单号	
	 */
	private String out_trade_no;
	
	/**
	 * 交易码
	 */
	private String out_trade_code;
	
	/**
	 * 收款人标识
	 */
	private String 	payee_identity_id;
	
	/**
	 * 收款人标识类型
	 */
	private String 	payee_identity_type;
	
	/**
	 * 收款人账户类型
	 */
	private String account_type;
	
	/**
	 * 金额
	 */
	private String amount;
	
	/**
	 * 分账信息列表
	 * 参数间用“^”分隔，各条目之间用“|”分隔
	 */
	private String split_list;
	
	/**
	 * 摘要
	 */
	private String summary;
	
	/**
	 * 商户标号
	 */
	private String goods_id;
	
	/**
	 * 交易状态
	 */
	private String trade_status;
	
	/**
	 * 后续推进需要的参数
	 */
	private String ticket;
	
	/**
	 * 支付状态
	 */
	private String pay_status;
	
	
	/**
	 * 待签名字符数组
	 */
	private String[] signParamNames = new String[]{"version","service","request_time","partner_id","_input_charset","out_trade_no","out_trade_code","summary","payee_identity_id","payee_identity_type","account_type","amount","notify_url"};
	
	/**
	 * 提交字符数组
	 */
	private String[] subParamNames = new String[]{"version","service","request_time","partner_id","_input_charset","out_trade_no","out_trade_code","summary","payee_identity_id","payee_identity_type","account_type","amount","notify_url","sign_type","sign"};

	
	public void getParamNames(String split_list){
		if (StringUtil.isNotBlank(split_list)) {
			signParamNames = new String[]{"version","service","request_time","partner_id","_input_charset","out_trade_no","out_trade_code","summary","payee_identity_id","payee_identity_type","account_type","amount","split_list","notify_url"};
			subParamNames = new String[]{"version","service","request_time","partner_id","_input_charset","out_trade_no","out_trade_code","summary","payee_identity_id","payee_identity_type","account_type","amount","split_list","notify_url","sign_type","sign"};
		}
	}
	
	
	@Override
	public SinaModel response(String res) throws IOException {
		try {
			JSONObject json = JSON.parseObject(res);
			this.pay_status = json.getString("pay_status");
			this.trade_status = json.getString("trade_status");
			this.out_trade_no = json.getString("out_trade_no");
			this.ticket = json.getString("ticket");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 获取 交易订单号 
	 * @return 
	 */
	public String getOut_trade_no() {
		return out_trade_no;
	}

	/**
	 * 设置 交易订单号 
	 * @param 
	 */
	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	/**
	 * 获取 交易码
	 * @return 
	 */
	public String getOut_trade_code() {
		return out_trade_code;
	}

	/**
	 * 设置 交易码
	 * @param 
	 */
	public void setOut_trade_code(String out_trade_code) {
		this.out_trade_code = out_trade_code;
	}

	/**
	 * 获取 收款人标识
	 * @return 
	 */
	public String getPayee_identity_id() {
		return payee_identity_id;
	}

	/**
	 * 设置 收款人标识
	 * @param 
	 */
	public void setPayee_identity_id(String payee_identity_id) {
		this.payee_identity_id = payee_identity_id;
	}

	/**
	 * 获取 收款人标识类型
	 * @return 
	 */
	public String getPayee_identity_type() {
		return payee_identity_type;
	}

	/**
	 * 设置 收款人标识类型
	 * @param 
	 */
	public void setPayee_identity_type(String payee_identity_type) {
		this.payee_identity_type = payee_identity_type;
	}

	/**
	 * 获取 收款人账户类型
	 * @return 
	 */
	public String getAccount_type() {
		return account_type;
	}

	/**
	 * 设置 收款人账户类型
	 * @param 
	 */
	public void setAccount_type(String account_type) {
		this.account_type = account_type;
	}

	/**
	 * 获取 金额
	 * @return 
	 */
	public String getAmount() {
		return amount;
	}

	/**
	 * 设置 金额
	 * @param 
	 */
	public void setAmount(String amount) {
		this.amount = amount;
	}

	/**
	 * 获取 分账信息列表 参数间用“^”分隔，各条目之间用“|”分隔
	 * @return 
	 */
	public String getSplit_list() {
		return split_list;
	}

	/**
	 * 设置 分账信息列表 参数间用“^”分隔，各条目之间用“|”分隔
	 * @param 
	 */
	public void setSplit_list(String split_list) {
		this.split_list = split_list;
	}

	/**
	 * 获取 摘要
	 * @return 
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * 设置 摘要
	 * @param 
	 */
	public void setSummary(String summary) {
		this.summary = summary;
	}

	/**
	 * 获取 商户标号
	 * @return 
	 */
	public String getGoods_id() {
		return goods_id;
	}

	/**
	 * 设置 the 商户标号
	 * @param 
	 */
	public void setGoods_id(String goods_id) {
		this.goods_id = goods_id;
	}

	/**
	 * 获取 the signParamNames
	 * @return 
	 */
	public String[] getSignParamNames() {
		return signParamNames;
	}

	/**
	 * 设置 the signParamNames
	 * @param 
	 */
	public void setSignParamNames(String[] signParamNames) {
		this.signParamNames = signParamNames;
	}

	/**
	 * 获取 the subParamNames
	 * @return 
	 */
	public String[] getSubParamNames() {
		return subParamNames;
	}

	/**
	 * 设置 the subParamNames
	 * @param 
	 */
	public void setSubParamNames(String[] subParamNames) {
		this.subParamNames = subParamNames;
	}
	/**
	 * 获取 交易状态
	 * @return 
	 */
	public String getTrade_status() {
		return trade_status;
	}
	/**
	 * 设置 交易状态
	 * @param 
	 */
	public void setTrade_status(String trade_status) {
		this.trade_status = trade_status;
	}
	/**
	 * 获取 后续推进需要的参数
	 * @return 
	 */
	public String getTicket() {
		return ticket;
	}
	/**
	 * 设置 后续推进需要的参数
	 * @param 
	 */
	public void setTicket(String ticket) {
		this.ticket = ticket;
	}
	/**
	 * 获取 支付状态
	 * @return 
	 */
	public String getPay_status() {
		return pay_status;
	}
	/**
	 * 设置 支付状态
	 * @param 
	 */
	public void setPay_status(String pay_status) {
		this.pay_status = pay_status;
	}

	
	
}
